package com.example.stock.controller;

import com.example.stock.controller.vo.StockInfoVO;
import com.example.stock.excel.ETFConstruct;
import com.example.stock.model.MonthDetail;
import com.example.stock.service.EtfConstructService;
import com.example.stock.service.MonthDataService;
import com.example.stock.service.StockService;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("etfConstruct")
public class EtfConstructController {

    @Autowired
    EtfConstructService etfConstructService;

    @Autowired
    MonthDataService monthDataService;

    @Autowired
    StockService stockService;

    @GetMapping("{etfName}")
    public Result index(@PathVariable String etfName) {
        List<ETFConstruct> stocks = etfConstructService.getETFConstructByName(etfName);
        if (stocks == null || stocks.isEmpty()) {
            return ResultGenerator.genSuccessResult( stocks );
        }

        stocks.forEach(item -> {
            MonthDetail currentMonthDetail = monthDataService.getCurrentMonthDetail(item.getStock());
            if (currentMonthDetail != null) {
                item.setLp(currentMonthDetail.getLp());
            }

            StockInfoVO stockInfo = stockService.getStockInfo(item.getStock());
            if (stockInfo != null) {
                item.setWg(stockInfo.getWg());
            }
        });

        return ResultGenerator.genSuccessResult(
                stocks
        );
    }

    @GetMapping("stock/{stock}")
    public Result stock(@PathVariable String stock) {
        return ResultGenerator.genSuccessResult(
                etfConstructService.getETFNamesByStock(stock)
        );
    }
}
